HOW TO Place a dialog (or dialog_modal) window where you want it.
Over and over we see people wanting to know how to get a dialog window to display where they want it to, instead of where it wants to. This is the way that I place a dialog window, where I want it.
To begin with I remove the VISIBLE style from the window.
Stylebits #main, 0, _WS_VISIBLE, 0, 0
Next we set up our window, it’s contents and open it. With the VISIBLE style removed the window will not show.
WindowWidth = 305
WindowHeight = 100
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
Button #main.OK, "Save", [newFilter], UL, 85, 25
Button #main.close, "Close", [unFilter], UL, 165, 25
Open "Edit Stage List" For dialog_modal As #main
Print #main, "font fixedsys 9"
Print #main, "trapclose [unFilter]"
We finish out the initial code by getting the handle of our dialog window, and calling the subs that will move and show our window.
fHwnd=Hwnd(#main)
Call locateWindow fHwnd, UpperLeftX, UpperLeftY, WindowWidth, WindowHeight
Call showItNow, fHwnd
Wait
Now the important part. The sub to relocate our dialog window. We will use the Windows API call MoveWindow found in user32.dll. Our sub is called locateWindow.
Sub locateWindow hWnd, ULx, ULy, WW, WH
Calldll #user32, "MoveWindow",_
hWnd As ULong,_ 'the handle to our dialog window
ULx As Long,_ 'upper left x for our window placement
ULy As Long,_ 'upper left y for our window placement
WW As Long,_ 'our window width
WH As Long,_ 'our window height
1 As Long,_ 'repaint=1
success As Long 'return value non-zero denotes success
End Sub
Last but not least, we will show our window, now that we have moved it to where we like it. This is done in the showItNow sub, using the Windows API call ShowWindow found in user32.dll.
Sub showItNow hWnd
Calldll #user32, "ShowWindow",_
hWnd As ULong,_ 'the handle to our dialog window
1 As Long,_ '1=show 0=hide
success As Long 'return value non-zero denotes success
End Sub
I hope this makes life, (or at least programming), easier for someone.
Ken
DEMO
Stylebits #main, 0, _WS_VISIBLE, 0, 0
WindowWidth = 305
WindowHeight = 100
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
Button #main.OK, "Save", [newFilter], UL, 85, 25
Button #main.close, "Close", [unFilter], UL, 165, 25
Open "Edit Stage List" For dialog_modal As #main
Print #main, "font fixedsys 9"
Print #main, "trapclose [unFilter]"
fHwnd=Hwnd(#main)
Call locateWindow fHwnd, UpperLeftX, UpperLeftY, WindowWidth, WindowHeight
Call showItNow, fHwnd
Wait
[newFilter] wait
[unFilter] close #main:end
Sub locateWindow hWnd, ULx, ULy, WW, WH
Calldll #user32, "MoveWindow",_
hWnd As ULong,_ 'the handle to our dialog window
ULx As Long,_ 'upper left x for our window placement
ULy As Long,_ 'upper left y for our window placement
WW As Long,_ 'our window width
WH As Long,_ 'our window height
1 As Long,_ 'repaint=1
success As Long 'return value non-zero denotes success
End Sub
Sub showItNow hWnd
Calldll #user32, "ShowWindow",_
hWnd As ULong,_ 'the handle to our dialog window
1 As Long,_ '1=show 0=hide
success As Long 'return value non-zero denotes success
End Sub